/*
 * 版权：Copyright PING AN INSURANCE (GROUP) COMPANY OF CHINA ,LTD. All Rights Reserved.
 * 项目名称：cloud-ucp-im-admin
 * 创建人： XIWEICHENG497
 * 创建时间：2018 下午2:23:54
 */
package com.xiweicheng.test.springboot.im.admin.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xiweicheng.test.springboot.im.admin.entity.security.User;
import com.xiweicheng.test.springboot.im.admin.model.RespBody;
import com.xiweicheng.test.springboot.im.admin.service.UserService;
import com.xiweicheng.test.springboot.im.admin.vo.UserVo;

/**
 * TODO〈功能描述〉
 * 
 * @author XIWEICHENG497
 */
@CrossOrigin
@RestController
@RequestMapping("auth")
public class AuthController {

    @Autowired
    UserService userService;

    @PostMapping("login")
    public RespBody login(@RequestBody UserVo user) {

        String token = userService.login(user.getUsername(), user.getPassword());

        return RespBody.succeed(token);

    }

    @PostMapping(value = "register")
    public RespBody register(@RequestBody UserVo user) {

        User user2 = userService.register(new User(user.getUsername(), user.getPassword(), true));

        return RespBody.succeed(user2);
    }

    @GetMapping(value = "refreshToken")
    public String refreshToken(@RequestHeader("Authorization") String authorization) {

        return userService.refreshToken(authorization);
    }

}
